package com.itheima.service.ipml;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.Name;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    // @Override
    // public PageBean page(Integer page, Integer pageSize) {
    //     //1、获取总记录数
    //     Long count = empMapper.count();
    //
    //     //2、获取分页查询结果列表
    //     Integer start = (page -1) * pageSize;
    //     List<Emp> empList = empMapper.list(start,pageSize);
    //
    //     //3、封装PageBean对象
    //     PageBean pageBean = new PageBean(count,empList);
    //     return pageBean;
    // }


    /**
     * 使用插件分页
     */
    @Override
    @Log
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行条件分页查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        //获取查询结果
        Page<Emp> p = (Page<Emp>) empList;
        //封装PageBean
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /**
     * 删除数据
     * @param ids
     */
    @Override
    @Log
    public void deleteEmp(List<Integer> ids) {
        empMapper.deleteEmp(ids);
    }

    /**
     * 新增员工数据
     * @param emp
     */
    @Override
    @Log
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
    }

    @Override
    @Log
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    /**
     * 更新员工数据
     */
    @Override
    @Log
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    /**
     * 登录
     * @param emp
     * @return
     */

    @Override
    public Emp loginEmp(Emp emp) {
       Emp loginEmp = empMapper.loginEmp(emp);
        return loginEmp;
    }


}
